package com.lin.manager.config;

import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.lin.manager.common.convert.StrToDateConverter;
import com.lin.manager.config.properties.IgnoreProperties;
import com.lin.manager.interceptor.UserInfoInterceptor;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

/**
 * @作者: 林江
 * @创建时间: 2024/3/11
 * @功能:
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Resource
    IgnoreProperties properties;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new UserInfoInterceptor())
                .excludePathPatterns(properties.getUrl())
                .addPathPatterns("/**");
    }

    /**
     * 自定义转换器
     * 注意：这里配置的转换器都是前端传入进来的数据不符合后端的规范，所以才需要自定义转换
     *
     * @param registry
     */
    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new StrToDateConverter());
    }


    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
        return jacksonObjectMapperBuilder -> jacksonObjectMapperBuilder.serializerByType(Long.class, ToStringSerializer.instance).serializerByType(Long.TYPE, ToStringSerializer.instance);
    }

//    @Bean
//    public CorsFilter corsWebFilter() {
//        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
//        CorsConfiguration corsConfiguration = new CorsConfiguration();
//        //1、配置跨域
//        corsConfiguration.addAllowedHeader("*");
//        corsConfiguration.addAllowedMethod("*");
//        corsConfiguration.addAllowedOriginPattern("*");
//        corsConfiguration.setAllowCredentials(true);
//
//        source.registerCorsConfiguration("/**", corsConfiguration);
//        return new CorsFilter(source);
//    }

}
